package com.lovo.view.school;

import java.util.List;

import com.lovo.bean.CutPageBean;
import com.lovo.bean.SchoolBean;
import com.lovo.javafxComponent.*;
import com.lovo.service.ISchoolService;
import com.lovo.service.impl.SchoolServiceImpl;

import javafx.scene.layout.Pane;

/**
 * 学校信息面板
 */
public class SchoolPane extends Pane {

    /**
     * 学校表格
     */
    private LovoTable table;
    /**
     * 学校名称查询文本框
     */
    private LovoTxt nameTxt;

    /**状态选择下拉框*/
    private LovoComboBox statusBox;
    /**
     * 分页对象
     */
    private LovoCutPage cutObj;
    
    private ISchoolService schoolService = new SchoolServiceImpl();

    /**
     * 构造方法
     */
    public SchoolPane() {
        this.initComponent();
    }

    /**
     * 初始化组件
     */
    public void initComponent() {
        new LovoTitle("学校管理", 0, 30, this);

        cutObj = new LovoCutPage(50, 350, this) {
            @Override
            public void clickEvent(int pageNO) {
                findSchoolInfo(pageNO);
            }
        };

        LovoButton addButton = new LovoButton("录入新学校", 80, 420, this);
        addButton.setOnAction(e -> showAddDialog());
        LovoButton delButton = new LovoButton("删除学校", 220, 420, this);
        LovoButton updateButton = new LovoButton("修改学校信息", 80, 470, this);
        LovoButton showInfoButton = new LovoButton("查看学校信息", 220, 470, this);
        LovoButton applyForButton = new LovoButton("申请立项", 80, 520, this);
        LovoButton auditingButton = new LovoButton("审核", 220, 520, this);

        addButton.setOnAction(e -> showAddDialog());
        updateButton.setOnAction(e -> showUpdateDialog());
        showInfoButton.setOnAction(e -> showInfoDialog());
        applyForButton.setOnAction(e -> showApplyForDialog());
        auditingButton.setOnAction(e -> showAuditingDialog());
        delButton.setOnAction(e -> deleteSchool());

        LovoTitlePane tp = new LovoTitlePane("查询学校", 350, 410, 400, 165, this);
        this.nameTxt = new LovoTxt("学校名称", 50, 30, tp);
        this.statusBox = new LovoComboBox("状态",
                new String[] {"","接洽中","待审","审核未通过","推广开展"},50, 80, tp);


        LovoButton findButton = new LovoButton("查询", 150, 130, tp);
        findButton.setOnAction(e -> findSchoolInfo(1));
        initTable();
    }

    /**
     * 显示添加对话框
     */
    private void showAddDialog() {
        new AddSchoolDialog(this);
    }
    /**
     * 显示修改对话框
     */
    private void showUpdateDialog() {
        new UpdateSchoolDialog(this);
    }
    /**
     * 显示学校信息对话框
     */
    private void showInfoDialog() {
        new ShowSchoolDialog(this);
    }

    /**
     *  申请立项
     */
    private void showApplyForDialog(){
    	if(table.getSelectItem() == null) {
    		LovoDialog.showMessageDialog("请选择需要申请立项的学校");
    		return;
    	}
    	SchoolBean schoolBean = (SchoolBean)table.getSelectItem();
    	// 修改学校状态的方法
    	schoolService.updateStatus(schoolBean.getId(), "待审");
        LovoDialog.showMessageDialog(schoolBean.getName() + "学校申请立项已提交，等审批");
        findSchoolInfo(1);
    }

    /**
     * 审核
     */
    private void showAuditingDialog(){

    	SchoolBean schoolBean = (SchoolBean) table.getSelectItem();
    	if(schoolBean == null) {
    		LovoDialog.showMessageDialog("请选择需要审核的学校");
    		return;
    	}
    	if(schoolBean.getStatus().equals("待审")) {
    		new AuditingDialog(this,schoolBean);
    	}else{
    		LovoDialog.showMessageDialog("学校必须为待审状态才能进行审批");
    	}
        
    }
    
    private void deleteSchool() {
    	
    }

    //------------------------

    /**
     * 初始化表格
     */
    private void initTable() {
        this.table = new LovoTable<>(new String[]{
                "学校名称:name", "校长:leader","城市,city", "状态:status"
        }, 10, 80, 780, 260, this);
        this.findSchoolInfo(1);
    }

    /**
     * 查询学校信息
     */
    public void findSchoolInfo(int pageNO) {
    	CutPageBean cutPageBean = schoolService.findByItem(pageNO, nameTxt.getText() , statusBox.getValue().toString());
    	table.updateData(cutPageBean.getList());
    	if(pageNO == 1) {
    		cutObj.setTotal(cutPageBean.getTotalPage());;
    	}

    }

}
